package sapling.api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sapling.dao.DrawLandAreaDao;
import sapling.dao.RoleDao;
import sapling.model.DrawLandArea;
import sapling.service.PushService;
import sapling.service.StorageService;
import sapling.service.TreeService;
import sapling.utils.Msg;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/16.
 */
@RestController
@RequestMapping("/api/storage")
public class StorageApi {
    @Autowired
    private StorageService storageService;
    @Autowired
    private TreeService treeService;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PushService pushService;
    @Autowired
    private DrawLandAreaDao drawLandAreaDao;

    @ApiOperation(value = "苗木出库", notes = "treeids以;分隔,操作人name")
    @PostMapping("/out")
    public Msg<String> out(@RequestParam String treeids, @RequestParam String name) {
        List<DrawLandArea> all = drawLandAreaDao.findAll();
        int out = storageService.out(treeids, name);
        pushService.inform("finance", "有" + out + "棵苗木需要出库审核");
        return Msg.newMsg("出库" + out + "棵");
    }

    @ApiOperation(value = "苗木号苗", notes = "treeids以;分隔,name号苗人名字,days过期天数如2017-10-01,direction 去向")
    @PostMapping("/mark")
    public Msg<String> mark(@RequestParam String treeids, @RequestParam String name, @RequestParam String days, @RequestParam(value = "direction", required = false) String direction) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(days);
        } catch (ParseException e) {
            e.printStackTrace();
            return Msg.newMsg("时间格式非法");
        }
        int out = storageService.mark(treeids, name, date, direction, 1);
        return Msg.newMsg("号苗" + out + "棵");
    }

    @ApiOperation(value = "苗木死苗", notes = "treeids以;分隔,name标记人名字")
    @PostMapping("/die")
    public Msg<String> die(@RequestParam String treeids, @RequestParam String name) {
        List<DrawLandArea> all = drawLandAreaDao.findAll();
        int out = storageService.die(treeids, name);
        pushService.inform("finance", "有" + out + "棵苗木死亡需要确认");
        return Msg.newMsg("标记死苗" + out + "棵");
    }

    @ApiOperation(value = "清空地块", notes = "清空", hidden = true)
    @GetMapping("/out2")
    public Msg<String> hh(@RequestParam int landid) {
        if (landid == 4) {
            DrawLandArea drawLandArea = new DrawLandArea();
            drawLandArea.setMark("die");
            drawLandAreaDao.save(drawLandArea);
        } else if (landid == 5) {
            List<DrawLandArea> all = drawLandAreaDao.findAll();
            for (DrawLandArea drawLandArea : all) {
                drawLandArea.setMark("mark");
                drawLandAreaDao.saveAndFlush(drawLandArea);
            }
        }
        return Msg.newMsg("success");
    }
}
